package com.ruoyi.quartz.controller;

import com.alibaba.fastjson2.JSON;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.exception.job.TaskException;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.quartz.domain.IotJob;
import com.ruoyi.quartz.service.IIotJobService;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletResponse;
import org.quartz.SchedulerException;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 定时任务Controller
 * 
 * @author changkang
 * @date 2025-05-01
 */
@RestController
@RequestMapping("/iotJob/iotJob")
public class IotJobController extends BaseController {

    @Resource
    private IIotJobService iotJobService;

    /**
     * 查询定时任务列表
     */
    @PreAuthorize("@ss.hasPermi('iotJob:iotJob:list')")
    @GetMapping("/list")
    public TableDataInfo list(IotJob iotJob)
    {
        startPage();
        List<IotJob> list = iotJobService.selectIotJobList(iotJob);
        return getDataTable(list);
    }

    /**
     * 导出定时任务列表
     */
    @PreAuthorize("@ss.hasPermi('iotJob:iotJob:export')")
    @Log(title = "定时任务", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, IotJob iotJob)
    {
        List<IotJob> list = iotJobService.selectIotJobList(iotJob);
        ExcelUtil<IotJob> util = new ExcelUtil<IotJob>(IotJob.class);
        util.exportExcel(response, list, "定时任务数据");
    }

    /**
     * 获取定时任务详细信息
     */
    @PreAuthorize("@ss.hasPermi('iotJob:iotJob:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(iotJobService.selectIotJobById(id));
    }

    /**
     * 新增定时任务
     */
    @PreAuthorize("@ss.hasPermi('iotJob:iotJob:add')")
    @Log(title = "定时任务", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody IotJob iotJob) throws SchedulerException, TaskException {
        iotJob.setUserId(getUserId());
        try {
            JSON.parseObject(iotJob.getParams());
        } catch (Exception e) {
            return error("新增失败'" + iotJob.getName()+ "'失败，params格式不正确");
        }
        return toAjax(iotJobService.insertIotJob(iotJob));
    }

    /**
     * 修改定时任务
     */
    @PreAuthorize("@ss.hasPermi('iotJob:iotJob:edit')")
    @Log(title = "定时任务", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody IotJob iotJob) throws SchedulerException, TaskException {
        try {
            JSON.parseObject(iotJob.getParams());
        } catch (Exception e) {
            return error("新增失败'" + iotJob.getName()+ "'失败，params格式不正确");
        }
        return toAjax(iotJobService.updateIotJob(iotJob));
    }

    /**
     * 删除定时任务
     */
    @PreAuthorize("@ss.hasPermi('iotJob:iotJob:remove')")
    @Log(title = "定时任务", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) throws SchedulerException {
        return toAjax(iotJobService.deleteIotJobByIds(ids));
    }

    /**
     * 定时任务立即执行一次
     */
    @PreAuthorize("@ss.hasPermi('iotJob:iotJob:changeStatus')")
    @Log(title = "定时任务", businessType = BusinessType.UPDATE)
    @PutMapping("/run")
    public AjaxResult run(@RequestBody IotJob iotJob) throws SchedulerException {
        boolean result = iotJobService.run(iotJob);
        return result ? success() : error("任务不存在或已过期！");
    }

    /**
     * 定时任务状态修改
     */
    @PreAuthorize("@ss.hasPermi('iotJob:iotJob:changeStatus')")
    @Log(title = "定时任务", businessType = BusinessType.UPDATE)
    @PutMapping("/changeStatus")
    public AjaxResult changeStatus(@RequestBody IotJob iotJob) throws SchedulerException, TaskException {
        return toAjax(iotJobService.changeStatus(iotJob));
    }
}
